// 月视图模块
export function initMonthView(views, currentDate, invoke) {
  return {
    updateMonthView,
    renderMonthCalendar
  };

  // 更新月视图
  async function updateMonthView() {
    const year = currentDate.getFullYear();
    const month = currentDate.getMonth() + 1;
    document.getElementById('current-month').textContent = `${year}年${month}月`;

    try {
      const todos = await invoke('get_todos_by_month', { year, month });
      renderMonthCalendar(todos);
    } catch (error) {
      console.error('加载月视图数据失败:', error);
    }
  }

  // 渲染月历
  function renderMonthCalendar(todos) {
    const grid = document.getElementById('calendar-grid');
    grid.innerHTML = '';

    // 获取当月第一天是星期几
    const firstDay = new Date(currentDate.getFullYear(), currentDate.getMonth(), 1).getDay();
    // 获取当月天数
    const daysInMonth = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1, 0).getDate();
    // 获取今天
    const today = new Date();

    // 添加星期标题
    const weekdays = ['日', '一', '二', '三', '四', '五', '六'];
    weekdays.forEach(day => {
      const header = document.createElement('div');
      header.className = 'calendar-day-header';
      header.textContent = day;
      grid.appendChild(header);
    });

    // 添加空白格子
    for (let i = 0; i < firstDay; i++) {
      const empty = document.createElement('div');
      empty.className = 'calendar-day';
      grid.appendChild(empty);
    }

    // 添加日期格子
    for (let day = 1; day <= daysInMonth; day++) {
      const date = new Date(currentDate.getFullYear(), currentDate.getMonth(), day);
      const dayElement = document.createElement('div');
      dayElement.className = 'calendar-day';
      if (date.toDateString() === today.toDateString()) {
        dayElement.classList.add('today');
      }
      dayElement.addEventListener('click', () => {
        currentDate = new Date(date);
        views.month.classList.remove('active');
        views.day.classList.add('active');
      });

      const header = document.createElement('div');
      header.className = 'calendar-day-header';
      header.textContent = day;
      dayElement.appendChild(header);

      const todosContainer = document.createElement('div');
      todosContainer.className = 'calendar-day-todos';
      dayElement.appendChild(todosContainer);

      // 添加该日期的 Todo
      const dayTodos = todos.filter(todo => {
        const todoDate = new Date(todo.due_time || todo.start_time);
        return todoDate.getDate() === day &&
               todoDate.getMonth() === currentDate.getMonth() &&
               todoDate.getFullYear() === currentDate.getFullYear();
      });

      dayTodos.forEach(todo => {
        const todoElement = document.createElement('div');
        todoElement.className = `calendar-todo importance-${todo.importance.toLowerCase()}`;
        if (todo.completed) {
          todoElement.classList.add('completed');
        }
        todoElement.textContent = todo.title;
        todoElement.addEventListener('click', (e) => {
          e.stopPropagation();
          views.month.classList.remove('active');
          views.todos.classList.add('active');
          // 滚动到对应的todo项
          setTimeout(() => {
            const todoEl = document.querySelector(`[data-todo-id="${todo.id}"]`);
            if (todoEl) {
              todoEl.scrollIntoView({ behavior: 'smooth', block: 'center' });
              todoEl.classList.add('highlight');
              setTimeout(() => todoEl.classList.remove('highlight'), 2000);
            }
          }, 100);
        });
        todoElement.addEventListener('dblclick', (e) => {
          e.stopPropagation();
          // TODO: 需要从主模块导入showTodoModal
        });
        todoElement.dataset.todoId = todo.id;
        todosContainer.appendChild(todoElement);
      });

      grid.appendChild(dayElement);
    }
  }
}